package com.maolong.controller.manger;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maolong.entity.domain.User;
import com.maolong.entity.enumm.IdentityEnum;
import com.maolong.entity.format.Code;
import com.maolong.entity.format.RespResult;
import com.maolong.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/manger")
public class MangerController {

    @Autowired
    private LoginService loginService;

    /**
     * 获取全部的工作人员记录，或者特定名称的
     * @param name
     * @return
     */
    @GetMapping
    public RespResult manger(@RequestParam(required = false,value = "name") String name){
        log.info("name:{}",name);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("identify",2);
        List<User> list=new ArrayList<>();
        if (name==null||"".equals(name)){
            list=loginService.list(queryWrapper);
        }else{
            queryWrapper.like("username",name);
            list=loginService.list(queryWrapper);
        }

        System.out.println(list);
        return new RespResult(list!=null?Code.USER_OK:Code.USER_FAIL,list,null);
    }

    /**
     * 录入工作人员
     * @param user
     * @return
     */
    @PostMapping
    public RespResult mangerPost(@RequestBody User user){
        user.setIdentify(IdentityEnum.WORKER);
        boolean save = loginService.save(user);
        return new RespResult(save==true?Code.USER_OK:Code.USER_FAIL,save,null);
    }

    /**
     * 删除人员
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespResult mangerDelete(@PathVariable(value = "id") Long id){
        log.info("id:{}",id);
        boolean removeById = loginService.removeById(id);
        return new RespResult(removeById==true?Code.USER_OK:Code.USER_FAIL,removeById,null);
    }

    /**
     * 修改人员
     * @param user
     * @return
     */
    @PutMapping
    public RespResult mangerPut(@RequestBody User user){
        boolean updateById = loginService.updateById(user);
        return new RespResult(updateById==true? Code.USER_OK:Code.USER_FAIL,updateById,null);
    }
}
